class Cart {
  constructor() {
    this.cartData = localStorage.getItem("cartData")
      ? JSON.parse(localStorage.getItem("cartData"))
      : {};
  }
  saveData(id, num, terminal) {
    if (!this.cartData[id] || terminal) {
      this.cartData[id] = num;
    } else {
      this.cartData[id] += num;
    }

    localStorage.setItem("cartData", JSON.stringify(this.cartData));
  }
  showList(domobj) {
    this.domobj = domobj;
    let prodDatas = JSON.parse(localStorage.getItem("prodDatas"));
    let cartData = JSON.parse(localStorage.getItem("cartData"));

    let str = "";
    for (let id in cartData) {
      str += `
      <li data-id="${id}">
      <input type="checkbox" class="ck" />
      <img src="${prodDatas[id].imgsrc}" />
      <span>${prodDatas[id].title}</span>
      <span class="price">${prodDatas[id].price}</span>
      <span class="minus">-</span>
      <input type="text" class="num" value="${cartData[id]}" />
      <span class="plus">+</span>
      <span class="perPrices">${prodDatas[id].price * cartData[id]}</span>
      <span class="del">删除</span>
    </li>
      `;
    }
    domobj.innerHTML = str;
  }
  checkAll(id) {
    let oCheckAll = document.getElementById("checkAll");
    let aCks = document.querySelectorAll(".ck");
    oCheckAll.onclick = () => {
      for (let i = 0; i < aCks.length; i++) {
        aCks[i].checked = oCheckAll.checked;
      }
      this.getTotalPrice("totalPrice");
    };
    for (let i = 0; i < aCks.length; i++) {
      aCks[i].onclick = () => {
        var count = 0;
        for (let j = 0; j < aCks.length; j++) {
          if (aCks[j].checked) {
            count++;
          }
        }
        if (count == aCks.length) {
          oCheckAll.checked = true;
        } else {
          oCheckAll.checked = false;
        }
        this.getTotalPrice("totalPrice");
      };
    }
  }
  updateData() {
    let _this = this;
    this.aMinus = document.querySelectorAll(".minus");
    this.aPlus = document.querySelectorAll(".plus");
    this.aNums = document.querySelectorAll(".num");
    this.aPrice = document.querySelectorAll(".price");
    this.aPerPrice = document.querySelectorAll(".perPrices");
    this.aCks = document.querySelectorAll(".ck");

    for (let i = 0; i < this.aMinus.length; i++) {
      //减
      this.aMinus[i].onclick = () => {
        let id = this.aMinus[i].parentNode.getAttribute("data-id");
        //console.log(id);
        if (this.aNums[i].value <= 1) {
          this.aNums[i].value = 1;
          return;
        }
        this.aNums[i].value--;

        this.saveData(id, Number(this.aNums[i].value), true);
        getPerPrice(i);
        this.getTotalPrice("totalPrice");
      };
      //加
      this.aPlus[i].onclick = () => {
        let id = this.aPlus[i].parentNode.getAttribute("data-id");
        this.aNums[i].value++;
        this.saveData(id, Number(this.aNums[i].value), true);
        getPerPrice(i);
        this.getTotalPrice("totalPrice");
      };
      //改
      this.aNums[i].onchange = () => {
        let id = this.aNums[i].parentNode.getAttribute("data-id");
        if (this.aNums[i].value <= 1) {
          this.aNums[i].value = 1;
        }
        this.saveData(id, Number(this.aNums[i].value), true);
        getPerPrice(i);
        this.getTotalPrice("totalPrice");
      };
    }

    function getPerPrice(i) {
      _this.aPerPrice[i].innerText =
        _this.aNums[i].value * _this.aPrice[i].innerText;
    }
  }
  getTotalPrice(id) {
    let oTotalPrice = document.getElementById(id);
    let totalPrice = 0;
    for (let j = 0; j < this.aCks.length; j++) {
      if (this.aCks[j].checked) {
        totalPrice += Number(this.aPerPrice[j].innerText);
      }
    }
    oTotalPrice.innerText = totalPrice;
  }
  removeData() {
    let aDelbtns = document.querySelectorAll(".del");
    for (let i = 0; i < aDelbtns.length; i++) {
      aDelbtns[i].onclick = () => {
        let id = aDelbtns[i].parentNode.getAttribute("data-id");
        this.domobj.removeChild(aDelbtns[i].parentNode);
        delete this.cartData[id];
        localStorage.setItem("cartData", JSON.stringify(this.cartData));
        this.aCks[i].checked = false;
        this.getTotalPrice("totalPrice");
      };
    }
  }
}
